package com.wuhen.qcshop.controller;


import com.wuhen.qcshop.mapper.UserMapper;
import com.wuhen.qcshop.pojo.dto.UserLoginDto;
import com.wuhen.qcshop.pojo.dto.UserRegDto;
import com.wuhen.qcshop.pojo.entity.User;
import com.wuhen.qcshop.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController

public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/reg")
    public int reg(@RequestBody UserRegDto user){
        UserVO u = mapper.selectByUsername(user.getUsername());//获取用户名存在u上
        if (u!=null){
            return 2;//
        }
        User userEntity = new User();
        //把dto里面的数据装进实体类
        BeanUtils.copyProperties(user,userEntity);
        //把实体里面的数据保存到数据库里面
        mapper.insert(userEntity);
        return 1;//注册成功
    }
    @RequestMapping("/login")
    public int login(@RequestBody UserLoginDto user){
        System.out.println("user = " + user );
        UserVO u = mapper.selectByUsername(user.getUsername());
        if (u!=null){//如果用戶不等于空值
            if (u.getPassword().equals(user.getPassword())){
               return 1;//登录成功
            }
            return 2;//密码错误
        }
        return 3;//用户名不存在
    }

}
